package com.example.armmvvm.ui.customdraw.view

import android.content.Context
import android.graphics.*
import android.graphics.Paint.ANTI_ALIAS_FLAG
import android.util.AttributeSet
import android.view.View
import com.example.arm.ext.dp
import kotlin.math.cos
import kotlin.math.sin


/**
 * 仪表盘
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.main
 * @createTime 创建时间 ：2021/9/13
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */

private const val OPEN_ANGLE = 120f

class DashboardTestView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(ANTI_ALIAS_FLAG)

    // 弧度的path
    val path = Path()

    // 刻度宽
    private val scaleWidth = 2.dp

    // 刻度长
    private val scaleLength = 5.dp

    // 指针长
    private val arrowRadius = 50.dp

    // 仪表盘的宽度
    private val dashWidth = 200.dp

    private val halfDashWidth = dashWidth / 2

    private var startX = 0f

    private var endX = 0f

    private var endY = 0f

    private var startY = 0f

    private val dashCount = 10

    // 刻度的path
    private val dashPah = Path().apply {
        addRect(0f, 0f, scaleWidth, scaleLength, Path.Direction.CW)
    }

    // 刻度样式
    private lateinit var pathDashPathEffect: PathDashPathEffect

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        startX = width.toFloat() / 2 - halfDashWidth
        endX = width.toFloat() / 2 + halfDashWidth
        endY = height.toFloat() / 2 + halfDashWidth
        startY = height.toFloat() / 2 - halfDashWidth

        path.addArc(
            startX,
            startY,
            endX,
            endY,
            90 + OPEN_ANGLE / 2,
            360f - OPEN_ANGLE
        )
        val pathMeasure = PathMeasure(path, false)
        val fl = (pathMeasure.length - scaleWidth) / (dashCount - 1)
        pathDashPathEffect = PathDashPathEffect(dashPah, fl, 0f, PathDashPathEffect.Style.ROTATE)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        paint.color = Color.BLACK
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3.dp
        // 弧度
        canvas.drawPath(
            path,
            paint
        )
        // 刻度
        paint.pathEffect = pathDashPathEffect
        canvas.drawArc(
            startX,
            startY,
            endX,
            endY,
            90f + OPEN_ANGLE / 2,
            360f - OPEN_ANGLE,
            false,
            paint
        )
        paint.pathEffect = null

        // 指针
        val index = 5
        // 指针角度
        val angle = 90f + OPEN_ANGLE / 2 + index * (360f - OPEN_ANGLE) / (dashCount - 1)
        // 旋转canvas实现
//        val save = canvas.save()
//        canvas.rotate(angle, width.toFloat() / 2, height.toFloat() / 2)
//        canvas.drawLine(
//            width.toFloat() / 2,
//            height.toFloat() / 2,
//            width.toFloat() / 2 + arrowRadius,
//            height.toFloat() / 2,
//            paint
//        )
//        canvas.restore()

        // 通过余弦定理实现
        canvas.drawLine(
            width.toFloat() / 2,
            height.toFloat() / 2,
            (width.toFloat() / 2 + arrowRadius * cos(Math.toRadians(angle.toDouble()))).toFloat(),
            (height.toFloat() / 2 + arrowRadius * sin(Math.toRadians(angle.toDouble()))).toFloat(),
            paint
        )
    }
}